package main;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;

import screensaver.ScreenSaverWindow;

import worker.Worker;
import worker.workerInformation;

import comm.ConnectionType;
import comm.InfrastructureInformation;
import comm_2.Comm;
import comm_2.Streams;

public class Main {

	
	//********** VERSION ******************
	private final static String VERSION = "Version 1 April - Evolve";
	//*************************************
	private static int MAINSERVERPORT = 9999;

	private static String MAINSERVERADDRESS = "127.0.0.1";	 

	private ObjectInputStream inputStream;
	private ObjectOutputStream outputStream;
	private workerInformation localhostinfo;
	private InfrastructureInformation infrastructureInformation;
	private int numberOfProcessors = 0; //at least one
	private int myID;
	private final static int retryConnect = 5000;
	private final static boolean screenSaverMode = true;
	private ScreenSaverWindow sw; 
	private Worker worker = new Worker();
	
	public Main(String[] args, boolean screesaver) throws IOException, ClassNotFoundException, InterruptedException{

		super();

		if(args.length>1)
			numberOfProcessors = new Integer(args[1]);
		if (screenSaverMode) {
			sw = new ScreenSaverWindow(worker);
			sw.start();
		}
		execute();
		System.out.println(VERSION);

	}  


	public static void main(String[] args) {
		while(true){
			try {
				Main m = new Main(args,true);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("Connection closed! Trying to reconnect...");
			try {
				Thread.sleep(retryConnect);
			} catch (InterruptedException e) { }
		}
	}
	private void getLocalHostInfo(){
		Runtime runtime = Runtime.getRuntime();
		//		int numberOfProcessors;
		String operatingSystem;
		operatingSystem = System.getProperty("os.name");

		if(numberOfProcessors == 0)
			this.numberOfProcessors = runtime.availableProcessors();
		//		if (numberOfProcessors>0)
		//			this.numberOfProcessors = numberOfProcessors;
		try {
			localhostinfo = new workerInformation(numberOfProcessors, operatingSystem, InetAddress.getLocalHost());
			System.out.println(localhostinfo.toString());
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	          
	}


	private void execute() throws IOException, ClassNotFoundException, InterruptedException{
		getLocalHostInfo();


		doConnections();
		startNegotiations();


	}


	private void startNegotiations() throws IOException, ClassNotFoundException, InterruptedException{

		outputStream.writeObject(ConnectionType.JA_WORKER_HELLO);


		outputStream.writeObject(localhostinfo);
		this.myID = (Integer)inputStream.readObject();
		System.out.println("MY ID IS:" +this.myID);
		//RECEIVE INFRASTRUTURE INFORMATIONS
		infrastructureInformation = (InfrastructureInformation) inputStream.readObject();
		System.out.println("Received Infra struture information:" + infrastructureInformation.toString());

		worker.startWorker(infrastructureInformation ,System.out,this.myID,this.numberOfProcessors);

		while (true) {
			ConnectionType type = (ConnectionType)inputStream.readObject();
			switch(type){
			case PING:

				break;
			}		
		}

	}

	private void doConnections() throws IOException{
		Streams InOut = new Comm(MAINSERVERPORT, MAINSERVERADDRESS).startConnectionToServer();

		if(InOut!=null){
			this.inputStream = InOut.returnObjectInputStream();
			this.outputStream = InOut.returnObjectOutputStream();
		}
		else{
			System.out.println("Comm has problems!!");
		}
	}

} 


